/**********************************************************************************
Copyright (C), by AppleCai
Project                      : Study Kernel
Description                  : BSP level for cdev
History                      :
CPU and Compiler             : AM335x,ARM-LINIX-GCC
GenDate                      : 2020-11-25
OwnerBy                      : Apple Cai
history                      : 2020-11-26--删除ok打印。bufr从char改成int
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <asm/termios.h>

#define DEV_NAME  "/dev/AppleUS100"
int main (int argc, char *argv[])
{
	int fd;
	int len;
    char bufw[1] = {0x55};
	int bufr[1]={0};
	//fd = open(DEV_NAME, O_RDWR|O_NOCTTY|O_NONBLOCK);
	fd = open(DEV_NAME, O_RDONLY);
    if(fd < 0) {
		perror(DEV_NAME);
		return -1;
    }

    //printf("ok\n");
    len = read(fd, bufr, 1);
		if (len < 0) {
			perror("read");
			close(fd);
			printf("%d\n",len);
			return -1;
		}
		else
		{
			printf("distance is %ld\n",bufr[0]);
		}
	#if 0
	while(1)
	{
		# if 1
		len = write(fd, bufw, 1);
		if (len < 0) {
			printf("write data error \n");
		}
		else
		{
			printf("write 0x55\n");
		}
		#endif
		len = read(fd, bufr, 2);
		if (len < 0) {
			perror("read");
			close(fd);
			printf("%d\n",len);
			return -1;
		}
		else
		{
			printf("distance is %d\n", bufr[0]*256+bufr[1]);
		}
		sleep(5);
	
	}
	#endif
	sleep(5);
	close(fd);
	return(0);
	
}